package com.youkeyi.ddy.cloud.basic.pub.mapping.controller;


import com.youkeyi.ddy.cloud.basic.pub.mapping.entity.SysMapping;
import com.youkeyi.ddy.cloud.basic.pub.mapping.param.BusinessDeleteParam;
import com.youkeyi.ddy.cloud.basic.pub.mapping.param.BusinessQueryParam;
import com.youkeyi.ddy.cloud.basic.pub.mapping.service.SysMappingService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.PublicMappingCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.param.PublicMappingBusinessUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.param.PublicMappingQueryCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 映射关系表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-02-19
 */
@Service
public class SysMappingController implements PublicMappingCloudService {

    @Autowired
    private SysMappingService sysMappingService;

    @Override
    public PublicMappingListCloudDto query(PublicMappingQueryCloudParam param) {
        PublicMappingListCloudDto dto = new PublicMappingListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        BusinessQueryParam businessQueryParam = new BusinessQueryParam();
        businessQueryParam.setMappingType(param.getMappingType());
        businessQueryParam.setKeyType(param.getKeyType());
        businessQueryParam.setKeys(param.getKeys());
        List<SysMapping> mappingList = sysMappingService.businessQuery(businessQueryParam);
        if (CollectionUtils.isNotEmpty(mappingList)) {

            List<PublicMappingListItemCloudDto> items = new ArrayList<>();
            mappingList.forEach(mapping -> {
                PublicMappingListItemCloudDto item = new PublicMappingListItemCloudDto();
                item.setMappingPrimaryKey(mapping.getPrimaryKey());
                item.setMappingType(mapping.getMappingType());
                item.setSourceKey(mapping.getSourceKey());
                item.setTargetKey(mapping.getTargetKey());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent businessUpdate(PublicMappingBusinessUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getMappingType()) || StringUtils.isBlank(param.getKeyType())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        // 删除映射数据
        BusinessDeleteParam deleteMappingParam = new BusinessDeleteParam();
        deleteMappingParam.setMappingType(param.getMappingType());
        deleteMappingParam.setKeyType(param.getKeyType());
        deleteMappingParam.setKeys(Collections.singletonList(param.getKeyTypeKey()));
        sysMappingService.businessDelete(deleteMappingParam);

        // 映射数据录入
        if (CollectionUtils.isNotEmpty(param.getChangeKeys())) {
            List<SysMapping> serverCreateParams = new ArrayList<>();
            param.getChangeKeys().forEach(changeKey -> {
                SysMapping serverMapping = new SysMapping();
                serverMapping.setPrimaryKey(SimpleUUidUtils.getUuid());
                serverMapping.setMappingType(param.getMappingType());
                serverMapping.setSourceKey(param.getKeyTypeKey());
                serverMapping.setTargetKey(changeKey);
                serverMapping.setCreateUserKey(param.getCreateUserKey());
                serverMapping.setCreateUserName(param.getCreateUserName());
                serverCreateParams.add(serverMapping);
            });
            sysMappingService.businessCreate(serverCreateParams);
        }

        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return rc;
    }

}
